import kfp
from kfp_notebook.pipeline import NotebookOp


@kfp.dsl.pipeline(
   name='{{ pipeline_name }}',
   description='{{ pipeline_description }}'
)

def create_pipeline():
    {% for key, operation in operations_list.items() %}
        {% set op_name = operation.notebook_name.split('.') %}
        {% set operation_id = op_name[0] | replace ("/","_") %}
            notebook_op_{{ operation_id }} = NotebookOp(name='{{ operation_id }}',
                                                          notebook='{{ operation.notebook_name }}',
                                                          cos_endpoint='{{ operation.cos_endpoint }}',
                                                          cos_bucket='{{ operation.cos_bucket }}',
                                                          cos_directory='{{ operation.cos_directory }}',
                                                          cos_dependencies_archive='{{ operation.cos_dependencies_archive }}',
                                                          pipeline_inputs='{{ operation.pipeline_inputs }}',
                                                          pipeline_outputs='{{ operation.pipeline_outputs }}',
                                                          image='{{ operation.image }}')

            notebook_op_{{ operation_id }}.name = '{{ operation.name }}'
            notebook_op_{{ operation_id }}.env_variables = {{ operation.env_variables }}
            notebook_op_{{ operation_id }}.dependent_names = {{ operation.dependent_names }}

    {% endfor %}


if __name__ == "__main__":
    pipeline_func = create_pipeline
    pipeline_filename = '{{ pipeline_name }}' + '.pipeline.tgz'
    kfp.compiler.Compiler().compile(pipeline_func, pipeline_filename)

    client = kfp.Client(host='{{ api_endpoint }}')
    name = '{{ pipeline_name }}'
    exp = client.create_experiment('{{ pipeline_name }}')
    run_result = client.run_pipeline(exp.id, name, pipeline_filename, {})
